package com.qd.p2p.web.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.Action;

import com.qdlc.p2p.biz.service.ArticleService;
import com.qdlc.p2p.biz.service.BorrowService;
import com.qdlc.p2p.biz.service.BorrowTenderService;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.SystemConfigHelper;
import com.qdlc.p2p.dal.common.SystemConfigHelper.Nid;
import com.qdlc.p2p.dal.dto.Borrow;
import com.qdlc.p2p.dal.dto.Site;
import com.qdlc.p2p.dal.dto.UserCache;
import com.qdlc.p2p.dal.model.ArticleModel;
import com.qdlc.p2p.dal.model.BorrowTenderModel;
import com.qdlc.p2p.dal.model.Tree;
import com.qdlc.p2p.dal.util.SystemConfigs;
import com.qdlc.p2p.dal.util.UserUtils;

/**
 * 论坛数据通信Action
 * 
 * @author xxx
 * @version 2.0
 * @date 2015年4月17日 下午5:09:25
 */
@SuppressWarnings("rawtypes")
public class BBSAction extends BaseAction {
	@Resource
	private ArticleService articleService;
	@Resource
	private BorrowTenderService borrowTenderService;
	@Resource
	private BorrowService borrowService;
	
	private Map<String, Object> data = new HashMap<String, Object>();
	/**
	 * 访问论坛加载初始数据
	 */
	@Action("/bbs/connect")
	public void connect() {
		Tree<Site> tree = articleService.getSiteTree();
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("tree", tree);
			map.put("web_url", SystemConfigHelper.getValue(Nid.WEB_URL));
			map.put("image_server_url", SystemConfigHelper.getValue(Nid.IMAGE_SERVER_URL));
			map.put("customer_hotline", SystemConfigHelper.getValue(Nid.CUSTOMER_HOTLINE));
			map.put("web_name", SystemConfigs.WEB_NAME);
			map.put("address", SystemConfigHelper.getValue(Nid.ADDRESS));
			map.put("copyright", SystemConfigHelper.getValue(Nid.COPYRIGHT));
			map.put("web_record_number", SystemConfigHelper.getValue(Nid.WEB_RECORD_NUMBER));
			if (getSessionUser() != null) {
				UserCache userCache = UserUtils.getUserCacheByUserId(getSessionUser());
				map.put("userType", userCache.getUserType());
			}
			printJson(getStringOfJpaObj(map));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 网站公告
	 * 
	 * @throws Exception
	 */
	@Action("/bbs/articleList")
	public void articleList() throws Exception {
		String nid = paramString("nid");
		int size = paramInt("size");
		if (size > 20) {
			size = 10;
		}
		List<ArticleModel> list = articleService.listBySize(nid, size);
		data.put("data", list);
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 最新投资列表
	 * 
	 * @throws Exception
	 */
	@Action("/bbs/newTenderList")
	public void newTenderList() throws Exception {
		List<BorrowTenderModel> newTenderList = borrowTenderService.getNewTenderList();
		data.put("data", newTenderList);
		printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 投资排行榜
	 * 
	 * @throws Exception
	 */
	@Action("/bbs/rankList")
	public void rankList() throws Exception {
		//List<RankModel> list = borrowTenderService.getRankList();
		//data.put("data", list);
		//printJson(getStringOfJpaObj(data));
	}
	
	/**
	 * 借款标
	 * 
	 * @throws Exception
	 */
	@Action("/bbs/borrowJson")
	public void borrowJson() throws Exception {
		Borrow borrow = borrowService.getLastBorrow();
		data.put("account", borrow.getAccount());
		data.put("accountYes", borrow.getAccountYes());
		data.put("scales", borrow.getScales());
		data.put("timeLimit", borrow.getTimeLimit());
		data.put("borrowTimeType", borrow.getBorrowTimeType());
		data.put("borrowName", borrow.getName());
		data.put("uuid", borrow.getUuid());
		data.put("userName", borrow.getUser().getHideUserName());
		data.put("apr", borrow.getApr());
		data.put("status", borrow.getStatus());
		int isPwd = 0;
		if (StringUtil.isNotBlank(borrow.getPwd()))
			isPwd = 1;
		data.put("isPwd", isPwd);
		data.put("borrowType", borrow.getType());
		printJson(getStringOfJpaObj(data));
	}
}
